# -*- coding: utf8 -*-
# report.py
"""
Function attributes:
* load: boolean. Optional. If False the function will not load.
* name: string. Optional.
* priority: string. Optional. Three values are available: "high", "medium" or "low",
  default value: "medium".
* thread: boolean. Optional. If True, function will work by starting a thread.
* rule: string or regexp object. Required.
* aliases: list of string or regexp objects. Optional.
* fullmsg: boolean. Optional. If True, pattern is searched on whole message, else
  pattern is a command, the message must begin with a sign.

* access
* evttypes: must be tuple
* skip_sentinel: skip sentinel instance
* limit
* channels
* url
"""
import re
from time import strftime as timestamp
from core.ircfuncs import is_channel, quiet_nick
from util.functions import status

field = "report"

def join_report(bot, evt, cmd):
	evt.chan = evt.target = evt.params[0].strip()
	if evt.chan and bot.channels[evt.chan].report:
		bot.ctrl_msg(evt, "%s has joined %s" % (evt.source, evt.chan))
join_report.rule = ["join"]
join_report.evttypes = ("join",)

def leave(bot, evt, cmd):
	if evt.source != bot.current_nname:
		if is_channel(evt.target) and evt.target != bot.defchan:
			if bot.channels[evt.target].report:
				bot.ctrl_msg(evt,
					"%s has leaved %s: %s" % (
						quiet_nick(evt.source, bot.channels[bot.defchan].users()),
						evt.target,
						evt.msg
					)
				)
			if bot.channels[evt.target].log:
				bot.log("%s has leaved %s: %s", evt.source, evt.target, evt.msg)
		else:
			rpt = []
			for chan in bot.channels:
				if evt.target in bot.channels[chan].users():
					rpt.append(bot.channels[chan].report)
			report = eval(" and ".join([str(x) for x in rpt])) if rpt else False
			if report:
				bot.ctrl_msg(evt,
					"%s has leaved %s: %s",
					quiet_nick(evt.source, bot.channels[bot.defchan].users()),
					bot.features.network(),
					evt.msg
				)
				bot.log("%s has leaved %s: %s", evt.source, bot.features.network(), evt.msg)
	else:
		if is_channel(evt.target) and evt.target != bot.defchan:
			if not bot.channels.has_key(evt.target) or bot.channels[evt.target].report:
				bot.ctrl_msg(evt,
					"%s has leaved %s: %s" % (
						quiet_nick(evt.source, bot.channels[bot.defchan].users()),
						evt.target,
						evt.msg
					)
				)
			if not bot.channels.has_key(evt.target) or bot.channels[evt.target].log:
				bot.log("%s has leaved %s: %s" % (evt.source, evt.target, evt.msg))
				pass
leave.rule = ["part", "quit", "disconnect"]
leave.evttypes = ("part", "quit", "disconnect")

def report(bot, evt, cmd):
	if evt.source != bot.current_nname:
		if is_channel(evt.target):
			if bot.channels[evt.target].report:
				bot.ctrl_msg(evt, "{%s} <%s> %s" % (evt.target, quiet_nick(evt.source, bot.channels[bot.defchan].users()), evt.msg))
			if bot.channels[evt.target].log:
				bot.log("%s {%s} <%s> %s" % (timestamp("[%H:%M:%S]"), evt.target, evt.source, evt.msg))
		else:
			if hasattr(bot, "features") and evt.source not in ("NickServ", "ChanServ"):
				bot.ctrl_msg(evt, "<PRIVMSG> <%s> %s" % (evt.source, evt.msg))
				bot.log("%s {%s} <%s> %s" % (timestamp("[%H:%M:%S]"), evt.target, evt.source, evt.msg))
report.evttypes = "*"
report.fullmsg = True

def wm_verbose(bot, evt, cmd):
	wmBot = bot.bots(("slave", 1))
	if not hasattr(wmBot, "verbose"):
		wmBot.verbose = True
	elif wmBot.verbose:
		wmBot.verbose = False
	else:
		wmBot.verbose = True
	bot.reply(evt, "Verbosity about neibourgh newpages turned %s" % status(wmBot.verbose))
wm_verbose.rule = "verbose"

def sendall(bot, evt, cmd):
	if not has_access(bot, sendall.level, evt): return True
	if not hasattr(bot, "sendall"):
		bot.sendall=True
	elif not cmd.arguments and bot.sendall==True:
		bot.sendall=False
	elif not cmd.arguments and bot.sendall==False:
		bot.sendall=True
	elif cmd.arguments:
		if cmd.arguments.lower() in ("1", "on", "true"):
			bot.sendall=True
		elif cmd.arguments.lower() in ("0", "off", "false"):
			bot.sendall=False
		else:
			bot.reply(evt, "sendall status is %s" % status(bot.sendall))
			return True
	bot.reply(evt, "sendall switched to %s" % status(bot.sendall))
sendall.rule = "sendall"
sendall.level = "admin"

def mode_(bot, evt, cmd):
	mBot = bot.bots(("master", 0))
	evt.source = "ChanServ" if evt.agent == "server" else evt.source
	print evt.target
	if len(evt.params)==1:
		bot.ctrl_msg(evt, "{source} gives {msg} to {params[0]}" . format (**evt.keys()))
	elif len(evt.params)>2:
		bot.ctrl_msg(evt, "{source} gives {params[1]} to {params[2]} on {params[0]}" . format (**evt.keys()))
	else:
		bot.ctrl_msg(evt, "{source} gives {params[1]} to {params[0]}" . format (**evt.keys()))
mode_.rule = ["mode"]
mode_.evttypes = ("mode",)
